home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / Filelist ƒ / Utilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-14  |  15.2 KB  |  778 lines

  1. /*
  2.     FileList 1.4
  3.     "Utilities.c"
  4. */
  5.  
  6. #include "Main.h"
  7. #include "Stack.h"
  8. #include "Search.h"
  9. #include "Utilities.h"
  10.  
  11. /* ----- Convert character to upper case ------------------------------- */
  12.  
  13. short toupper (register unsigned char c)
  14. {
  15.     return ((c >= 'a') && (c <= 'z')) ? (c - ('a' - 'A')) : c;
  16. }
  17.  
  18. /* ----- Compare strings ----------------------------------------------- */
  19.  
  20. short StrCompare (
  21. register unsigned char *s1,
  22. register unsigned char *s2)
  23. {
  24.     register short d, i, min;
  25.     register short c1, c2;
  26.  
  27.     d = *s1 - *s2;
  28.     min = (d < 0) ? *s1 : *s2;
  29.     for (i = 1; i <= min; i++) {
  30.         s1++;
  31.         s2++;
  32.         c1 = toupper(*s1);
  33.         c2 = toupper(*s2);
  34.         if (c1 != c2)
  35.             return c1 - c2;
  36.     }
  37.     return d;
  38. }
  39.  
  40. /* ----- Match strings ------------------------------------------------- */
  41.  
  42. static Boolean Match (
  43.     register Byte *s,    /* Pascal string */
  44.     register Byte *p)    /* Array */
  45. {
  46.     register short n = *s++;    /* String length */
  47.  
  48.     while (n-- > 0)
  49.         if (toupper(*s++) != toupper(*p++))
  50.             return FALSE;        /* No match */
  51.     return TRUE;                /* s matches p */
  52. }
  53.  
  54. Boolean StrIncludes (
  55.     register Byte *s,    /* Pascal string */
  56.     register Byte *p)    /* Pascal string */
  57. {
  58.     register short n = *p++ - *s;
  59.  
  60.     while (n-- >= 0)
  61.         if (Match(s, p++))
  62.             return TRUE;
  63.     return FALSE;
  64. }
  65.  
  66. Boolean StrEnds (
  67.     register Byte *s,    /* Pascal string */
  68.     register Byte *p)    /* Pascal string */
  69. {
  70.     if (!*p || *s > *p)
  71.         return FALSE;
  72.     return Match(s, p + (*p - *s + 1));
  73. }
  74.  
  75. Boolean StrBegins (
  76.     register Byte *s,    /* Pascal string */
  77.     register Byte *p)    /* Pascal string */
  78. {
  79.     if (!*p || *s > *p)
  80.         return FALSE;
  81.     return Match(s, p + 1);
  82. }
  83.  
  84. Boolean StrEquals (
  85.     register Byte *s,    /* Pascal string */
  86.     register Byte *p)    /* Pascal string */
  87. {
  88.     if (*p != *s)
  89.         return FALSE;
  90.     return Match(s, p + 1);
  91. }
  92.  
  93. /* ----- Fill memory --------------------------------------------------- */
  94.  
  95. void FillMemory (
  96.     register unsigned char *p,
  97.     register long n,
  98.     register unsigned char c)
  99. {
  100.     while(n--)
  101.         *p++ = c;
  102. }
  103.  
  104. /* ----- Right justify string ------------------------------------------ */
  105.  
  106. void RightJustify (
  107.     register unsigned char *s,
  108.     register short w,
  109.     register unsigned char c)
  110. {
  111.     register short d, n;
  112.     
  113.     if ((d = w - (n = *s)) > 0) {
  114.         *s++ = w;
  115.         BlockMove(s, s + d, (long)n);
  116.         while(d--)
  117.             *s++ = c;
  118.     }
  119. }
  120.  
  121. /* ----- Left justify string ------------------------------------------- */
  122.  
  123. void LeftJustify (
  124.     register unsigned char *s,
  125.     register short w,
  126.     register unsigned char c)
  127. {
  128.     register short d, n;
  129.  
  130.     if ((d = w - (n = *s)) > 0) {
  131.         *s++ = w;
  132.         s += n;
  133.         while(d--)
  134.             *s++ = c;
  135.     }
  136. }
  137.  
  138. /* ----- Append string2 to string1 ------------------------------------- */
  139.  
  140. void Append (
  141.     register unsigned char *s1,
  142.     register unsigned char *s2)
  143. {
  144.     register short d, n, i;
  145.  
  146.     n = *s1;
  147.     d = *s2;
  148.     i = n + d - 255;
  149.     if (i > 0)
  150.         d -= i;
  151.     *s1 = n + d;
  152.     BlockMove(s2 + 1, s1 + n + 1, d);
  153. }
  154.  
  155. /* ----- Convert integer to 2 digits ----------------------------------- */
  156.  
  157. void str2 (
  158.     register unsigned short i,
  159.     register unsigned char *s)
  160. {
  161.     register unsigned char str[7];
  162.  
  163.     NumToString((long)i, str);
  164.     if (i < 10) {
  165.         s[0] = '0';
  166.         s[1] = str[1];
  167.     } else {
  168.         s[0] = str[1];
  169.         s[1] = str[2];
  170.     }
  171. }
  172.  
  173. /* ----- Convert date and time to string ------------------------------- */
  174.  
  175. long date2str (
  176.     long sec,
  177.     register unsigned char *s,
  178.     char sep)
  179. {
  180.     register unsigned char *p;
  181.     short date[7];
  182.  
  183.     Secs2Date(sec, &date);
  184.     date[0] -= 1900;
  185.     p = s;
  186.     str2(date[DateFormat[0]], s); s += 2; *s++ = '/';
  187.     str2(date[DateFormat[1]], s); s += 2; *s++ = '/';
  188.     str2(date[DateFormat[2]], s); s += 2; *s++ = sep;
  189.     str2(date[3], s); s += 2; *s++ = ':';
  190.     str2(date[4], s); s += 2; *s++ = ':';
  191.     str2(date[5], s); s += 2;
  192.     return s-p;
  193. }
  194.  
  195. /* ----- Make sure string is printable --------------------------------- */
  196.  
  197. void printable (
  198.     register unsigned char *p,
  199.     register short n)
  200. {
  201.     register unsigned char c;
  202.  
  203.     while (n--) {
  204.         c = *p;
  205.         if (c < 0x20 || c == 0x7F)
  206.             *p = 0xC9;
  207.         ++p;
  208.     }
  209. }
  210.  
  211. /* ----- Convert volume info to string (spaces) ------------------------ */
  212.  
  213. void VolumeToString1 (
  214.     unsigned long i,
  215.     register unsigned char *s)
  216. {
  217.     register FileInfoPtr p;
  218.     register unsigned char n[10];
  219.     unsigned char *t;
  220.  
  221.     if (i >= VolumeData.count) {
  222.         s[0] = 0;
  223.         return;
  224.     }
  225.     FillMemory(s, 256L, ' ');
  226.     p = Address(&VolumeData, i);
  227.     t = s;
  228.     s++;    /* Skip length byte */
  229.  
  230.     /* Folder name */
  231.     BlockMove(p->name + 1, s, (long)(p->name[0]));
  232.     printable(s, p->name[0]);
  233.     s += 28;
  234.  
  235.     /* Available space */
  236.     NumToString(p->type, n);
  237.     BlockMove(n+1, s+(9-n[0]), (long)n[0]);
  238.     s += 10;
  239.  
  240.     /* Total space */
  241.     NumToString(p->size, n);
  242.     BlockMove(n+1, s+(9-n[0]), (long)n[0]);
  243.     s += 10;
  244.  
  245.     /* Number of files */
  246.     NumToString(p->creator, n);
  247.     BlockMove(n+1, s+(6-n[0]), (long)n[0]);
  248.     s += 7;
  249.  
  250.     /* Dates */
  251.     s += date2str(p->cdate, s, ' ') + 1;
  252.     s += date2str(p->mdate, s, ' ');
  253.  
  254.     *t = s - t - 1;        /* String length */
  255. }
  256.  
  257. /* ----- Convert volume info to string (tabs) -------------------------- */
  258.  
  259. void VolumeToString2 (
  260.     unsigned long i,
  261.     register unsigned char *s)
  262. {
  263.     register FileInfoPtr p;
  264.     register unsigned char n[10];
  265.     register short length;
  266.     unsigned char *t;
  267.  
  268.     if (i >= VolumeData.count) {
  269.         s[0] = 0;
  270.         return;
  271.     }
  272.     FillMemory(s, 256L, '\t');
  273.     p = Address(&VolumeData, i);
  274.     t = s;
  275.     s++;    /* Skip length byte */
  276.  
  277.     /* Folder name */
  278.     BlockMove(p->name + 1, s, (long)(length = p->name[0]));
  279.     printable(s, length);
  280.     s += length+1;
  281.  
  282.     /* Available space */
  283.     NumToString(p->type, n);
  284.     BlockMove(n+1, s, (long)(length = n[0]));
  285.     s += length+1;
  286.  
  287.     /* Total space */
  288.     NumToString(p->size, n);
  289.     BlockMove(n+1, s, (long)(length = n[0]));
  290.     s += length+1;
  291.  
  292.     /* Number of files */
  293.     NumToString(p->creator, n);
  294.     BlockMove(n+1, s, (long)(length = n[0]));
  295.     s += length+1;
  296.  
  297.     /* Dates */
  298.     s += date2str(p->cdate, s, '\t') + 1;
  299.     s += date2str(p->mdate, s, '\t');
  300.  
  301.     *t = s - t - 1;        /* String length */
  302. }
  303.  
  304. /* ----- Convert file info to string (spaces) -------------------------- */
  305.  
  306. void FileToString1 (
  307.     unsigned long i,
  308.     register unsigned char *s)
  309. {
  310.     register FileInfoPtr p;
  311.     register unsigned char *t;
  312.     register short length;
  313.     unsigned char n[10];
  314.     unsigned char *path;
  315.     long x;
  316.     STACK stack;
  317.  
  318.     if (i >= FileData.count) {
  319.         s[0] = 0;
  320.         return;
  321.     }
  322.     FillMemory(s, 256L, ' ');
  323.     p = Address(&FileData, i);
  324.     t = s;
  325.     s++;    /* Skip length byte */
  326.  
  327.     /* File name */
  328.     BlockMove(p->name + 1, s, (long)(p->name[0]));
  329.     printable(s, p->name[0]);
  330.     s += 32;
  331.  
  332.     /* Type */
  333.     x = p->type;
  334.     printable((unsigned char *)&x, sizeof(long));
  335.     BlockMove(&x, s, sizeof(long));
  336.     s += 5;
  337.  
  338.     /* Creator */
  339.     x = p->creator;
  340.     printable((unsigned char *)&x, sizeof(long));
  341.     BlockMove(&x, s, sizeof(long));
  342.     s += 5;
  343.  
  344.     /* Size */
  345.     NumToString(p->size, n);
  346.     BlockMove(n+1, s+(7-n[0]), (long)n[0]);
  347.     s += 8;
  348.  
  349.     /* Dates */
  350.     s += date2str(p->cdate, s, ' ') + 1;
  351.     s += date2str(p->mdate, s, ' ') + 1;
  352.  
  353.     /* Volume name and path */
  354.     InitPath(p, &stack);
  355.     length = 254 - (s - t - 1);
  356.     while ((path = NextPath(&stack)) && (*path <= length)) {
  357.         BlockMove(path+1, s, (long)(*path));
  358.         printable(s, *path);
  359.         s += *path;
  360.         *s++ = ':';
  361.         length -= *path + 1;
  362.     }
  363.  
  364.     *t = s - t - 1;        /* String length */
  365. }
  366.  
  367. /* ----- Convert file info to string (tabs) ---------------------------- */
  368.  
  369. void FileToString2 (
  370.     unsigned long i,
  371.     register unsigned char *s)
  372. {
  373.     register FileInfoPtr p;
  374.     register unsigned char *t;
  375.     register short length;
  376.     unsigned char n[10];
  377.     unsigned char *path;
  378.     long x;
  379.     STACK stack;
  380.  
  381.     if (i >= FileData.count) {
  382.         s[0] = 0;
  383.         return;
  384.     }
  385.     FillMemory(s, 256L, '\t');
  386.     p = Address(&FileData, i);
  387.     t = s;
  388.     s++;    /* Skip length byte */
  389.  
  390.     /* File name */
  391.     BlockMove(p->name + 1, s, (long)(length = p->name[0]));
  392.     printable(s, length);
  393.     s += length+1;
  394.  
  395.     /* Type */
  396.     x = p->type;
  397.     printable((unsigned char *)&x, sizeof(long));
  398.     BlockMove(&x, s, sizeof(long));
  399.     s += 5;
  400.  
  401.     /* Creator */
  402.     x = p->creator;
  403.     printable((unsigned char *)&x, sizeof(long));
  404.     BlockMove(&x, s, sizeof(long));
  405.     s += 5;
  406.  
  407.     /* Size */
  408.     NumToString(p->size, n);
  409.     BlockMove(n+1, s, (long)(length = n[0]));
  410.     s += length+1;
  411.  
  412.     /* Dates */
  413.     s += date2str(p->cdate, s, '\t') + 1;
  414.     s += date2str(p->mdate, s, '\t') + 1;
  415.  
  416.     /* Volume name and path */
  417.     InitPath(p, &stack);
  418.     path = NextPath(&stack);
  419.     BlockMove(path+1, s, (long)(*path));
  420.     s += *path + 1;
  421.     length = 254 - (s - t - 1);
  422.     while ((path = NextPath(&stack)) && (*path <= length)) {
  423.         BlockMove(path+1, s, (long)(*path));
  424.         printable(s, *path);
  425.         s += *path;
  426.         *s++ = ':';
  427.         length -= *path + 1;
  428.     }
  429.  
  430.     *t = s - t - 1;        /* String length */
  431. }
  432.  
  433. /* ----- Center dialog or alert template ------------------------------- */
  434.  
  435. void CenterDialog (
  436.     long templateType,    /* 'ALRT' or 'DLOG' */
  437.     short templateID)
  438. {
  439.     short screenWidth = Bounds.right - Bounds.left;
  440.     short screenHeight = Bounds.bottom - Bounds.top;
  441.     register Rect **h;    /* Templates start with boundsRect */
  442.     register Rect *p;
  443.     register short width, height;
  444.     
  445.     if (!(h = (Rect **)GetResource(templateType, templateID)))
  446.         return;
  447.     p = *h;
  448.     width = p->right - p->left;
  449.     height = p->bottom - p->top;
  450.     p->top = Bounds.top + (screenHeight - height)/3;
  451.     p->bottom = p->top + height;
  452.     p->left = Bounds.left + (screenWidth - width)/2;
  453.     p->right = p->left + width;
  454. }
  455.  
  456. /* ----- Where to put a dialog box so that it's centered --------------- */
  457.  
  458. void GetDlogOrigin (
  459.     short dlogID,
  460.     register Point *where)
  461. {
  462.     short screenWidth = Bounds.right - Bounds.left;
  463.     short screenHeight = Bounds.bottom - Bounds.top;
  464.     register DialogTHndl dlogHandle;
  465.     register Rect *drp;
  466.     register short dlogWidth, dlogHeight;
  467.  
  468.     dlogHandle = (DialogTHndl)GetResource('DLOG', dlogID);
  469.     if (!dlogHandle){
  470.         SetPt(where, 85, 85);
  471.         return;
  472.     }
  473.     drp = &((**dlogHandle).boundsRect);
  474.     dlogWidth = drp->right - drp->left;
  475.     dlogHeight = drp->bottom - drp->top;
  476.     where->h = Bounds.left + (screenWidth - dlogWidth)/2;
  477.     where->v = Bounds.top + (screenHeight - dlogHeight)/3;
  478. }
  479.  
  480. /* ----- Set control name ---------------------------------------------- */
  481.  
  482. void ControlName (
  483.     register DialogPtr dialog,
  484.     register short itemNo,
  485.     register unsigned char *name)
  486. {
  487.     short type;
  488.     ControlHandle control;
  489.     Rect box;
  490.  
  491.     GetDItem(dialog, itemNo, &type, &control, &box);
  492.     SetCTitle(control, name);
  493.     if (*name)
  494.         /*HiliteControl(control, 0);*/
  495.         ShowControl(control);
  496.     else
  497.         /*HiliteControl(control, 255);*/
  498.         HideControl(control);
  499. }
  500.  
  501. /* ----- Get control value --------------------------------------------- */
  502.  
  503. short ControlCheck (
  504.     register DialogPtr dialog,
  505.     register short itemNo)
  506. {
  507.     short type;
  508.     ControlHandle control;
  509.     Rect box;
  510.  
  511.     GetDItem(dialog, itemNo, &type, &control, &box);
  512.     return GetCtlValue(control);
  513. }
  514.  
  515. /* ----- Set control value --------------------------------------------- */
  516.  
  517. void ControlSet (
  518.     register DialogPtr dialog,
  519.     register short itemNo,
  520.     register short value)
  521. {
  522.     short type;
  523.     ControlHandle control;
  524.     Rect box;
  525.  
  526.     GetDItem(dialog, itemNo, &type, &control, &box);
  527.     SetCtlValue(control, value);
  528. }
  529.  
  530. /* ----- Toggle control value ------------------------------------------ */
  531.  
  532. void ControlToggle (
  533.     register DialogPtr dialog,
  534.     register short itemNo)
  535. {
  536.     short type;
  537.     ControlHandle control;
  538.     Rect box;
  539.  
  540.     GetDItem(dialog, itemNo, &type, &control, &box);
  541.     SetCtlValue(control, !GetCtlValue(control));
  542. }
  543.  
  544. /* ----- Set group of radio buttons ------------------------------------ */
  545.  
  546. Boolean SetRadioButton (
  547.     register DialogPtr dialog,
  548.     register short item1,
  549.     register short item2,
  550.     register short item)
  551. {
  552.     register short i;
  553.     short type;
  554.     Handle itemHdl;
  555.     Rect box;
  556.  
  557.     if (item < item1 || item > item2)
  558.         return FALSE;
  559.     for (i = item1; i <= item2; i++) {
  560.         GetDItem(dialog, i, &type, &itemHdl, &box);
  561.         SetCtlValue((ControlHandle)itemHdl, (item == i) ? 1 : 0);
  562.     }
  563.     return TRUE;
  564. }
  565.  
  566. /* ----- Get group of radio buttons ------------------------------------ */
  567.  
  568. short GetRadioButton (
  569.     register DialogPtr dialog,
  570.     register short item1,
  571.     register short item2)
  572. {
  573.     register short i;
  574.     short type;
  575.     Handle itemHdl;
  576.     Rect box;
  577.  
  578.     for (i = item1; i <= item2; i++) {
  579.         GetDItem(dialog, i, &type, &itemHdl, &box);
  580.         if (GetCtlValue((ControlHandle)itemHdl))
  581.             return i;
  582.     }
  583.     return 0;
  584. }
  585.  
  586. /* ----- Get dialog item text ------------------------------------------ */
  587.  
  588. void getText (
  589.     register DialogPtr dPtr,
  590.     short item,
  591.     unsigned char *str)
  592. {
  593.     short theType;
  594.     Handle theItem;
  595.     Rect theBox;
  596.  
  597.     GetDItem(dPtr, item, &theType, &theItem, &theBox);
  598.     GetIText(theItem, str);
  599. }
  600.  
  601. /* ----- Set dialog item text ------------------------------------------ */
  602.  
  603. void setText (
  604.     register DialogPtr dialogP,
  605.     short id,
  606.     unsigned char *s)
  607. {
  608.     short item;
  609.     Handle itemHdl;
  610.     Rect box;
  611.  
  612.     GetDItem(dialogP, id, &item, &itemHdl, &box);
  613.     SetIText(itemHdl, s);
  614. }
  615.  
  616. /* ----- Set dialog item number ---------------------------------------- */
  617.  
  618. void setNumber (
  619.     register DialogPtr dialogP,
  620.     short id,
  621.     long n)
  622. {
  623.     unsigned char s[10];
  624.  
  625.     NumToString(n, s);
  626.     setText(dialogP, id, s);
  627. }
  628.  
  629. /* ----- Get dialog item number ---------------------------------------- */
  630.  
  631. long getNumber (
  632.     register DialogPtr dialogP,
  633.     short id,
  634.     register long min,
  635.     register long max)
  636. {
  637.     long n;
  638.     register unsigned char s[256];
  639.  
  640.     getText(dialogP, id, s);
  641.     StringToNum(s, &n);
  642.     if (n < min)
  643.         n = min;
  644.     else
  645.         if (n > max)
  646.             n = max;
  647.     return n;
  648. }
  649.  
  650. /* ----- Show/hide dialog item ----------------------------------------- */
  651.  
  652. void ShowHideControl (
  653.     register DialogPtr dialog,
  654.     register short i,
  655.     register Boolean show)
  656. {
  657.     short type;
  658.     ControlHandle control;
  659.     Rect box;
  660.  
  661.     GetDItem(dialog, i, &type, &control, &box);
  662.     if (show)
  663.         ShowControl(control);
  664.     else
  665.         HideControl(control);
  666. }
  667.  
  668. /* ----- Draw user item in dialog box ---------------------------------- */
  669.  
  670. pascal void DrawICN (
  671.     register DialogPtr dialogP,
  672.     register short id)
  673. {
  674.     register Handle h;
  675.     short item;
  676.     Handle itemHdl;
  677.     Rect box;
  678.  
  679.     GetDItem(dialogP, id, &item, &itemHdl, &box);
  680.     if (h = GetResource('ICN#', 128)) {
  681.         HLock(h);
  682.         PlotIcon(&box, h);
  683.         HUnlock(h);
  684.     }
  685. }
  686.  
  687. /* ----- Frame item in dialog ------------------------------------------ */
  688.  
  689. pascal void FrameItem (
  690.     register DialogPtr window,
  691.     register short number)
  692. {
  693.     short type;
  694.     Handle item;
  695.     Rect box;
  696.  
  697.     GetDItem(window, number, &type, &item, &box);
  698.     FrameRect(&box);
  699.     MoveTo(box.left + 3, box.bottom);
  700.     LineTo(box.right, box.bottom );
  701.     LineTo(box.right, box.top + 3);
  702. }
  703.  
  704. /* ----- Set user item procedure --------------------------------------- */
  705.  
  706. void SetUserItem (
  707.     register DialogPtr dialog,
  708.     register short number,
  709.     register ProcPtr proc)
  710. {
  711.     short type;
  712.     Handle item;
  713.     Rect box;
  714.  
  715.     GetDItem(dialog, number, &type, &item, &box);
  716.     SetDItem(dialog, number, userItem, (Handle)proc, &box);
  717. }
  718.  
  719. /* ----- SF Dialog Hook to outline default button ---------------------- */
  720.  
  721. pascal short SFOutlineHook (
  722.     register short itemno,
  723.     register DialogPtr dialog)
  724. {
  725.     short type;
  726.     ControlHandle item;
  727.     Rect box;
  728.     unsigned char str[256];
  729.  
  730.     if (itemno == -1) {
  731.         /* Append '@' to name of Open/Save button */
  732.         GetDItem(dialog, getOpen, &type, &item, &box);
  733.         GetCTitle(item, str);
  734.         Append(str, (unsigned char *)"\p@");
  735.         SetCTitle(item, str);
  736.     }
  737.     return itemno;
  738. }
  739.  
  740. /* ----- Convert date formats ------------------------------------------ */
  741.  
  742. static short    Dates[][3] = {
  743.     { 2, 1, 0 },    /* day/month/year */
  744.     { 1, 2, 0 },    /* month/day/year */
  745.     { 0, 1, 2 },    /* year/month/day */
  746.     { 0, 0, 0 },
  747. };
  748.  
  749. void DFormat1 (register short n)
  750. {
  751.     register short i;
  752.  
  753.     for (i = 0; i < 3; i++)
  754.         DateFormat[i] = Dates[n][i];
  755. }
  756.  
  757. short DFormat2 (void)
  758. {
  759.     register short i, n;
  760.  
  761.     for (n = 0; n < 3; n++) {
  762.         for (i = 0; i < 3; i++)
  763.             if (DateFormat[i] != Dates[n][i])
  764.                 break;
  765.         if (i == 3)
  766.             return n;
  767.     }
  768.     return n;
  769. }
  770.  
  771. /* ----- Set watch cursor ---------------------------------------------- */
  772.  
  773. void SetWatch (void)
  774. {
  775.     RomMapInsert = mapTrue;
  776.     SetCursor(*GetCursor(watchCursor));    /* Should be in ROM */
  777. }
  778.